package cn.jiaju.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jiaju.pojo.User;
import cn.jiaju.pojo.cailiao;
import cn.jiaju.service.jiajuService;

@Controller
public class jiajucontroller {

	@Resource
	private jiajuService jiajuservice;

	@RequestMapping("/jiaju")
	public String jiaju() {

		return "login";
	}

	@RequestMapping("/jiajulogin")
	public String jiajulogin(String username, String password) {
		User u = jiajuservice.logins(username, password);
		if (u != null) {

			return "redirect:/jiajuframe";
		}

		return "login";
	}

	@RequestMapping("/jiajuframe")
	public String jiajuframe() {

		return "frame";
	}

	@RequestMapping("/verityAjax")
	@ResponseBody
	public int verityAjax(HttpServletRequest rq) {
		String username = rq.getParameter("username");
		int i = jiajuservice.verityAjax(username);

		return i;
	}

	@RequestMapping("/selectcaoliaos")
	public String selectcaoliaos(HttpServletRequest rq) {
		String pageIndex = rq.getParameter("pageIndex");
		if (pageIndex == null) {

			pageIndex = "1";
		}

		int pageCurNO = Integer.parseInt(pageIndex.trim());
		int getcount = jiajuservice.getcounts();
		System.out.println(">>>>>>>>>>>>>>" + getcount);
		int count = getcount % 3 == 0 ? getcount / 3 : getcount / 3 + 1;
		System.out.println("aaaaaaaaaaaaa" + count);
		List<cailiao> list = jiajuservice
				.selectcaoliaos((pageCurNO - 1) * 3, 3);
		System.out.println("PPPPPPPPPPPPP" + list.toString());
		rq.setAttribute("pageIndex", pageIndex);
		rq.setAttribute("getcount", getcount);

		rq.setAttribute("count", count);
		rq.setAttribute("list", list);

		return "selectcaoliaos";
	}

	@RequestMapping("/deletecailiao")
	public String delete(HttpServletRequest rq) {
		String dingdanId = rq.getParameter("dingdanId");

		int a = jiajuservice.Delete(Integer.parseInt(dingdanId));
		System.out.println("uuuuuuuu" + a);
		if (a > 0) {

			return "redirect:/selectcaoliaos";

		}

		return "delete";
	}

	@RequestMapping("/addcailiao")
	public String addcailiao() {

		return "addcailiao";
	}

	@RequestMapping("/Addcailiao")
	public String Addcailiao(cailiao cai) {
		int a = jiajuservice.Addcailiao(cai);
		if (a > 0) {

			return "redirect:/selectcaoliaos";
		}

		return "selectcaoliaos";
	}

}
